<?php

use think\migration\Migrator;

class UserScoreLog extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */

    protected string $name = 'user_score_log';

    public function up(): void
    {
        $table = $this->table($this->name, [
            'engine'      => 'InnoDB',
            'collation'   => 'utf8mb4_general_ci',
            'id'          => 'id',
            'signed'      => false,
            'primary_key' => ['id'],
            'comment'     => '用户积分变动记录'
        ]);
        $table->addColumn('module', 'string', [
            'limit' => 40, 'null' => false, 'comment' => '涉及模块',
        ])->addColumn('type', 'string', [
            'limit' => 30, 'null' => false, 'comment' => '类型',
        ])->addColumn('user_id', 'integer', [
            'limit' => 11, 'null' => false, 'comment' => '用户ID',
        ])->addColumn('flow', 'enum', [
            'values' => ['in', 'out'], 'default' => 'out', 'null' => false, 'comment' => '积分流向',
        ])->addColumn('score', 'integer', [
            'limit' => 11, 'null' => false, 'comment' => '变动积分',
        ])->addColumn('after', 'integer', [
            'limit' => 11, 'null' => false, 'comment' => '变动后总积分',
        ])->addColumn('remark', 'string', [
            'limit' => 200, 'default' => '', 'null' => false, 'comment' => '备注',
        ])->addColumn('create_at', 'datetime', [
            'null' => false, 'comment' => '记录时间',
        ])->addIndex(['flow', 'create_at'], [
            'name' => 'flow'
        ])->addIndex(['user_id', 'create_at'], [
            'name' => 'user'
        ])->save();
    }

    public function down(): void
    {
        // 删除表
        $table = $this->table($this->name);
        $table->exists() && $table->drop()->save();
    }
}
